package com.CST.service.impl.cb;

import com.CST.dto.cb.JxAssetValueDTO;
import com.CST.entity.cb.JxAssetValue;
import com.CST.mapper.cst.cb.JxCostMapper;
import com.CST.mapper.ecology.JxAssetValueMapper;
import com.CST.service.cb.JxAssetValueService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nmx
 * @since 2023-06-20
 */
@Service
@Slf4j
public class JxAssetValueServiceImpl extends ServiceImpl<JxAssetValueMapper, JxAssetValue> implements JxAssetValueService {

    @Autowired
    private JxAssetValueMapper jxAssetValueMapper;

    @Autowired
    private JxCostMapper jxCostMapper;

    @Override
    @Transactional
    public String getAssetValueInfo(JxAssetValueDTO dto) {
        //先杀后插
        int i = jxCostMapper.deleteByDate(dto);
        log.info("{}", "删除了" + i + "条");
        //查询三香用友库的固定资产原值数据
        List<JxAssetValue> list = jxAssetValueMapper.listAssetValue(dto);
        if (list.isEmpty()) {
            throw new RuntimeException("资产原值查询错误");
        }
        //插入
        int fromIndex = 0;
        List<JxAssetValue> insertList = new ArrayList<>();
        for (JxAssetValue item : list) {
            fromIndex++;
            insertList.add(item);
            if (fromIndex % 20 == 0 && fromIndex <= list.size()) {
                jxCostMapper.saveBatch(insertList);
                insertList = new ArrayList<>();
            }
        }
        if (CollectionUtils.isNotEmpty(insertList)) {
            jxCostMapper.saveBatch(insertList);
        }
        return "success";
    }
}
